/**
 * 
 */
package cn.gz.zoomtech.common.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.logicalcobwebs.proxool.configuration.JAXPConfigurator;


/**
 * 
 * @author chenhui
 *
 */
public class DBManager {

	private static DBManager instance;

	private static final Log log = LogFactory.getLog(DBManager.class);

	private DBManager() throws Exception {
		init();
	}


	/**
	 * 
	* package_name: cn.zoomtech.ejsms.db
	* file_name:    DBManager.java
	* description:  初始化，读取数据库的配置
	* 2011-5-31下午11:03:58
	* Author: chenhui
	 * @throws Exception
	 */
	private void init() throws Exception {
//		JAXPConfigurator.configure("proxool.xml", false);		
		return;
	}


	/**
	 * 
	* package_name: cn.zoomtech.ejsms.db
	* file_name:    DBManager.java
	* description: 单例模式，获取DBManager数据库管理的实例对象
	* 2011-5-31下午11:05:13
	* Author: chenhui
	 * @return
	 * @throws Exception
	 */
	public static synchronized DBManager getInstance() throws Exception {
		if (instance == null)
			instance = new DBManager();
		return instance;
	}


	/**
	 * 
	* package_name: cn.zoomtech.ejsms.db
	* file_name:    DBManager.java
	* description: 建立数据库连接池，连接池的名称为传入的别名
	* 2011-5-31下午11:06:13
	* Author: chenhui
	 * @param DbAlias
	 * @return
	 * @throws Exception
	 */
	public Connection getConnection(String DbAlias) throws Exception {
		
		Connection conn = DriverManager.getConnection("proxool."+DbAlias);
		return conn;
	}


	/**
	 * 
	* package_name: cn.zoomtech.ejsms.db
	* file_name:    DBManager.java
	* description: 建立默认的数据库别名连接池：proxool.DBPool
	* 2011-5-31下午11:07:24
	* Author: chenhui
	 * @return
	 * @throws Exception
	 */
	public Connection getConnection() throws Exception {
		
		Connection conn = DriverManager.getConnection("proxool.DBPool");
		return conn;
	}


	/**
	 * 
	* package_name: cn.zoomtech.ejsms.db
	* file_name:    DBManager.java
	* description: 释放链接
	* 2011-5-31下午11:08:29
	* Author: chenhui
	 * @param conn
	 */
	public void freeConnection(Connection conn){
		
		try{
			if(conn!=null){
				conn.close();
			}
			
		}catch(Exception ex){
			log.error("freeConnError:",ex);
		}
	}


	/**
	 * 
	* package_name: cn.zoomtech.ejsms.db
	* file_name:    DBManager.java
	* description: 主方法，测试用入口
	* 2011-5-31下午11:08:46
	* Author: chenhui
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			DBManager connMgr = DBManager.getInstance();
			
			Connection conn = connMgr.getConnection("DBPool");
			if (conn == null) {
				log.error("can't get connection DBPool");
				System.exit(1);
			}else{
			 
//				String	sql1 = " select sysdate from dual t  ";
				String	sql1 = " select * from sys_area t  ";
				boolean dbhave = false;
				ResultSet rs = null;
				PreparedStatement pstmt = null;
				log.info("sql1:" + sql1);
				pstmt = conn.prepareStatement(sql1);
				rs = pstmt.executeQuery();
				int count = 0;
				while (rs.next()) {
					dbhave = true;
					count++;
//					System.out.println(" value is:"+rs.getString("sysdate"));
				}				
//				log.info(count+",dbhave is:" + dbhave);
				conn.close();				
			}
			 
		} catch (Exception e) {
			e.printStackTrace();
			log.error("Exception is ".concat(String.valueOf(String
					.valueOf(e))));
		}
	}
}
